
#include "stdafx.h"
#include "AppTools.h"
#include "SettingHelper.h"

#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383


#define HKEY_PE_ROOT			_T("Software\\MailStat")
#define HKEY_PE_COMMON_SETTING	_T("Software\\MailStat\\Settings")
#define HKEY_PE_PHONE_SETTING	_T("Software\\MailStat\\DEVICE%s")


CSettingHelper::CSettingHelper(void)
{

}

CSettingHelper::~CSettingHelper(void)
{

}


void CSettingHelper::SetCommonSetting(PE_COMMON_SETTING *setting)
{
	HKEY hkey;

	if (RegOpenKeyEx(HKEY_CURRENT_USER, HKEY_PE_COMMON_SETTING, 0, KEY_ALL_ACCESS, &hkey)!= ERROR_SUCCESS)
	{
		RegCreateKey(HKEY_CURRENT_USER, HKEY_PE_COMMON_SETTING, &hkey);
	}

	RegSetValueEx(hkey, _T("dwVer"),				0,	REG_DWORD, (BYTE *)&(setting->dwVer),					sizeof(DWORD));
	RegSetValueEx(hkey, _T("dwIsRegistered"),		0,	REG_DWORD, (BYTE *)&setting->dwIsRegistered,			sizeof(DWORD));
	RegSetValueEx(hkey, _T("dwTrialStartTime"),		0,	REG_DWORD, (BYTE *)&setting->dwTrialStartTime,			sizeof(DWORD));
	RegSetValueEx(hkey, _T("szSN"),					0,	REG_SZ,	   (BYTE *)(setting->szSN),						(DWORD)_tcslen(setting->szSN)*sizeof(TCHAR));
	RegSetValueEx(hkey, _T("szPst"),			0,	REG_SZ,	   (BYTE *)(setting->szPst),				(DWORD)_tcslen(setting->szPst)*sizeof(TCHAR));

	RegSetValueEx(hkey, _T("dwUseDefaultPst"),	0,	REG_DWORD,	(BYTE *)&(setting->dwUseDefaultPst),	sizeof(DWORD));

	RegCloseKey(hkey);
}

int CSettingHelper::GetCommonSetting(PE_COMMON_SETTING *setting)
{
	HKEY  hkey;
	DWORD dwType;
	DWORD dwLen;

	if (RegOpenKeyEx(HKEY_CURRENT_USER, HKEY_PE_COMMON_SETTING, 0, KEY_ALL_ACCESS, &hkey) != ERROR_SUCCESS)
	{
		time_t ltime;
		time( &ltime );

		memset(setting,0,sizeof(PE_COMMON_SETTING));

		setting->dwVer = APP_VER;
		setting->dwTrialStartTime = (DWORD)ltime;
		setting->dwUseDefaultPst = 1;

		SetCommonSetting(setting);
		return PE_RET_FAIL;
	}

	dwLen = sizeof(DWORD);
	RegQueryValueEx(hkey, _T("dwVer"), 0, &dwType, (BYTE*)&(setting->dwVer), &dwLen);
	
	dwLen = sizeof(DWORD);
	RegQueryValueEx(hkey, _T("dwIsRegistered"), 0, &dwType, (BYTE*)&(setting->dwIsRegistered), &dwLen);

	dwLen = sizeof(DWORD);
	RegQueryValueEx(hkey, _T("dwTrialStartTime"), 0, &dwType, (BYTE*)&(setting->dwTrialStartTime), &dwLen);

	dwLen = 255 * sizeof(TCHAR);
	RegQueryValueEx(hkey, _T("szSN"), 0, &dwType, (BYTE*)(setting->szSN) ,&dwLen);

	dwLen = 255 * sizeof(TCHAR);
	RegQueryValueEx(hkey, _T("szPst"), 0, &dwType, (BYTE*)(setting->szPst) ,&dwLen);

	dwLen = sizeof(DWORD);
	RegQueryValueEx(hkey, _T("dwUseDefaultPst"), 0, &dwType, (BYTE*)&(setting->dwUseDefaultPst), &dwLen);

	RegCloseKey(hkey);

	if (setting->dwVer != APP_VER)
	{
		setting->dwVer = APP_VER;
		SetCommonSetting(setting);
	}

	return PE_RET_OK;
}





